package com.lqr.wechat.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

import com.bridge.imconfig.IMConst;
import com.bridge.imconfig.NimBlackListSDK;
import com.bridge.imconfig.NimFriendSDK;
import com.bridge.imconfig.NimUserInfoSDK;
import com.bridge.imconfig.imageloader.ImageLoaderManager;
import com.bridge.imconfig.utils.UserCache;
import com.lqr.wechat.R;
import com.lqr.wechat.databinding.ActivityUserInfoBinding;
import com.lqr.wechat.model.Contact;
import com.lqr.wechat.utils.StringUtils;
import com.lqr.wechat.utils.UIUtils;
import com.netease.nimlib.sdk.RequestCallback;
import com.netease.nimlib.sdk.uinfo.constant.GenderEnum;
import com.netease.nimlib.sdk.uinfo.model.NimUserInfo;

import java.util.List;
import java.util.Map;

/**
 * @创建者 CSDN_LQR
 * @描述 详细资料界面
 */
public class UserInfoActivity extends BaseActivity {

    public static final String USER_INFO_ACCOUNT = "account";
    private Intent mIntent;
    private Animation mPushBottomInAnimation;
    private Animation mPushBottomOutAnimation;
    private String mAccount;
    private Contact mContact;
    private ActivityUserInfoBinding binding ;

    private void jumpToAliasActivity() {
        mIntent = new Intent(UserInfoActivity.this, AliasActivity.class);
        mIntent.putExtra("contact", mContact);
        startActivityForResult(mIntent, AliasActivity.REQ_CHANGE_ALIAS);
    }
    @Override
    public void initListener() {
        binding.vMask.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toggleMenu();
            }
        });
        binding.oivAliasAndTag.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jumpToAliasActivity();
            }
        });
        binding.btnCheat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setResult(RESULT_OK);
                mIntent = new Intent(UserInfoActivity.this, SessionActivity.class);
                mIntent.putExtra(SessionActivity.SESSION_ACCOUNT, mAccount);
                startActivity(mIntent);
                finish();
            }
        });
        binding.btnVideoCheat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        binding.btnAddFriend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mIntent = new Intent(UserInfoActivity.this, PostscriptActivity.class);
                mIntent.putExtra("account", mAccount);
                startActivity(mIntent);
            }
        });
        binding.oivAlias.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //修改备注
                jumpToAliasActivity();
                hideMenu();
            }
        });
        binding.oivFriendsCirclePrivacySet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //修改朋友圈权限
                startActivity(new Intent(UserInfoActivity.this, FriendCirclePrivacySetActivity.class));
                hideMenu();
            }
        });
        binding.oivAddToBlackList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //加入黑名单
                hideMenu();
                showMaterialDialog("加入黑名单", "加入黑名单，你将不再收到对方的消息，并且你们互相看不到对方朋友圈的更新", "确定", "取消", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        NimBlackListSDK.addToBlackList(mAccount, new RequestCallback<Void>() {
                            @Override
                            public void onSuccess(Void param) {
                                UIUtils.showToast("加入黑名单成功");
                                Intent intent = new Intent(UserInfoActivity.this, MainActivity.class);
                                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                                startActivity(intent);
                            }

                            @Override
                            public void onFailed(int code) {
                                UIUtils.showToast("加入黑名单失败" + code);
                            }

                            @Override
                            public void onException(Throwable exception) {
                                exception.printStackTrace();
                            }
                        });
                        hideMaterialDialog();
                    }
                }, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        hideMaterialDialog();
                    }
                });
            }
        });

        binding.oivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //删除好友
                hideMenu();
                showMaterialDialog("删除联系人", "将联系人" + mContact.getDisplayName() + "删除，将同时删除与联系人的聊天记录", "删除", "取消", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //删除
                        NimFriendSDK.deleteFriend(mAccount, new RequestCallback<Void>() {
                            @Override
                            public void onSuccess(Void param) {
                                UIUtils.showToast("删除好友成功");
                                Intent intent = new Intent(UserInfoActivity.this, MainActivity.class);
                                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                                startActivity(intent);
                            }

                            @Override
                            public void onFailed(int code) {
                                UIUtils.showToast("删除好友失败" + code);
                            }

                            @Override
                            public void onException(Throwable exception) {
                                exception.printStackTrace();
                            }
                        });
                        hideMaterialDialog();
                    }
                }, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //取消
                        hideMaterialDialog();
                    }
                });
            }
        });
    }

    @Override
    protected int layoutId() {
        return R.layout.activity_user_info;
    }

    @Override
    public void init() {
        mAccount = getIntent().getStringExtra("account");
        if (TextUtils.isEmpty(mAccount)) {
            interrupt();
            return;
        }
    }

    @Override
    public void initView() {
        binding = getBind() ;
        initToolbar();
        initAnimation();

        if (UserCache.getAccount().equals(mAccount)) {//自己
            binding.oivAliasAndTag.setVisibility(View.GONE);
            binding.llArea.setVisibility(View.GONE);
            binding.llSignature.setVisibility(View.GONE);
            binding.btnCheat.setVisibility(View.VISIBLE);
        } else {
            if (NimFriendSDK.isMyFriend(mAccount)) {
                binding.btnCheat.setVisibility(View.VISIBLE);
                binding.oivAliasAndTag.setVisibility(View.VISIBLE);
//                mBtnVideoCheat.setVisibility(View.VISIBLE);
                binding.btnAddFriend.setVisibility(View.GONE);
            } else {
                binding.btnCheat.setVisibility(View.GONE);
                binding.oivAliasAndTag.setVisibility(View.GONE);
//                mBtnVideoCheat.setVisibility(View.GONE);
                binding.btnAddFriend.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public void initData() {
        //根据账号得到好友信息
        if (NimFriendSDK.isMyFriend(mAccount)) {
            mContact = new Contact(mAccount);
            //获取好友信息并显示
            setUserInfo();
            //先信息旧信息，同时更新新的好友信息
            getUserInfoFromServer();
        } else {
            getUserInfoFromServer();
        }
    }

    private void getUserInfoFromServer() {
        NimUserInfoSDK.getUserInfoFromServer(mAccount, new RequestCallback<List<NimUserInfo>>() {
            @Override
            public void onSuccess(List<NimUserInfo> param) {
                if (param != null && param.size() > 0) {
                    mContact = new Contact(NimFriendSDK.getFriendByAccount(mAccount), param.get(0));
                    //获取好友信息并显示
                    setUserInfo();
                }
            }

            @Override
            public void onFailed(int code) {

            }

            @Override
            public void onException(Throwable exception) {

            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (NimFriendSDK.isMyFriend(mAccount)) {
            new MenuInflater(this).inflate(R.menu.menu_more, menu);
        }
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int itemId = item.getItemId();
        if (itemId == android.R.id.home) {
            onBackPressed();
        } else if (itemId == R.id.itemMore) {
            toggleMenu();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == AliasActivity.REQ_CHANGE_ALIAS && resultCode == RESULT_OK) {
            //修改备注成功
            initData();
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onBackPressed() {
        if (binding.rlMenu.getVisibility() == View.VISIBLE) {
            //隐藏
            binding.svMenu.startAnimation(mPushBottomOutAnimation);
            return;
        }
        super.onBackPressed();
    }

    private void initToolbar() {
        setSupportActionBar(binding.lyToolbar.toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("详细资料");
        binding.lyToolbar.toolbar.setNavigationIcon(R.mipmap.ic_back);
    }

    private void initAnimation() {
        mPushBottomInAnimation = AnimationUtils.loadAnimation(this, R.anim.push_bottom_in);
        mPushBottomOutAnimation = AnimationUtils.loadAnimation(this, R.anim.push_bottom_out);
        mPushBottomInAnimation.setDuration(300);
        mPushBottomOutAnimation.setDuration(300);
        mPushBottomOutAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                binding.rlMenu.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    private void toggleMenu() {
        if (binding.rlMenu.getVisibility() == View.VISIBLE) {
            //隐藏
            binding.svMenu.startAnimation(mPushBottomOutAnimation);
        } else {
            //显示
            binding.rlMenu.setVisibility(View.VISIBLE);
            binding.svMenu.startAnimation(mPushBottomInAnimation);
        }
    }

    private void hideMenu() {
        binding.svMenu.startAnimation(mPushBottomOutAnimation);
    }

    private void setUserInfo() {
        //设置头像
        if (TextUtils.isEmpty(mContact.getAvatar())) {
            binding.ivHeader.setImageResource(R.mipmap.default_header);
        } else {
            ImageLoaderManager.LoadNetImage(mContact.getAvatar(), binding.ivHeader);
        }

        //设置性别
        NimUserInfo userInfo = mContact.getUserInfo();
        if (userInfo.getGenderEnum() == GenderEnum.FEMALE) {
            binding.ivGender.setImageResource(R.mipmap.ic_gender_female);
        } else if (userInfo.getGenderEnum() == GenderEnum.MALE) {
            binding.ivGender.setImageResource(R.mipmap.ic_gender_male);
        } else {
            binding.ivGender.setVisibility(View.GONE);
        }

        //判断是否有起备注
        if (TextUtils.isEmpty(mContact.getAlias())) {
//            binding.tvAlias.setVisibility(View.GONE);
            binding.tvName.setVisibility(View.GONE);
        } else {
//            binding.tvAlias.setVisibility(View.VISIBLE);
            binding.tvName.setVisibility(View.VISIBLE);
        }
        binding.tvAlias.setText(mContact.getDisplayName());
        binding.tvAccount.setText("微信号:" + mContact.getAccount());
        binding.tvName.setText("昵称:" + mContact.getName());
        Map<String, Object> extensionMap = mContact.getUserInfo().getExtensionMap();
        if (extensionMap != null)
            binding.tvArea.setText(StringUtils.isEmpty(extensionMap.get(IMConst.UserInfoExt.AREA)) ? "" : (String) extensionMap.get(IMConst.UserInfoExt.AREA));
            binding.tvSignature.setText(mContact.getUserInfo().getSignature());
    }
}
